package com.zhentao.controller;

import com.zhentao.Exception.vo.ResultVo;
import com.zhentao.pojo.SysUser;
import com.zhentao.service.AuthService;
import com.zhentao.vo.LoginVO;
import com.zhentao.vo.PhoneLoginVO;
import com.zhentao.vo.RegisterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

//import static org.yaml.snakeyaml.TypeDescription.log;

@Api(tags = "用户认证接口")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @ApiOperation(value = "用户登录", notes = "用户名和密码登录，返回token")
    @ApiResponses({
        @ApiResponse(code = 200, message = "登录成功"),
        @ApiResponse(code = 500, message = "登录失败")
    })
    @PostMapping("/login")
    public ResultVo<String> login(@RequestBody LoginVO loginVO) {
        return ResultVo.success(authService.login(loginVO));
    }

    @ApiOperation(value = "用户注册", notes = "注册新用户，默认为普通用户角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "注册成功"),
        @ApiResponse(code = 500, message = "注册失败")
    })
    @PostMapping("/register")
    public ResultVo<Void> register(@RequestBody RegisterVO registerVO) {
        authService.register(registerVO);
        return ResultVo.success();
    }

    @ApiOperation(value = "手机号验证码登录", notes = "使用手机号和验证码登录，返回token")
    @ApiResponses({
        @ApiResponse(code = 200, message = "登录成功"),
        @ApiResponse(code = 500, message = "登录失败")
    })
    @PostMapping("/phone/login")
    public ResultVo<String> phoneLogin(@RequestBody PhoneLoginVO phoneLoginVO) {
        return ResultVo.success(authService.phoneLogin(phoneLoginVO));
    }

    @ApiOperation(value = "发送验证码", notes = "向指定手机号发送验证码")
    @ApiResponses({
        @ApiResponse(code = 200, message = "发送成功"),
        @ApiResponse(code = 500, message = "发送失败")
    })
    @PostMapping("/send-code")
    public ResultVo<String> sendVerificationCode(@RequestParam String phone) {
//        log.info("发送验证码请求，手机号: {}", phone);
        authService.sendVerificationCode(phone);
        return ResultVo.success("验证码发送成功");
    }
}